package com.project.dao.impl;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean login(String userName, String password) {
        List list = null;
        this.setConnection();
        try {
            this.ps = this.connection.prepareStatement("select * from t_users where pk_name = ? and u_password = ?");
            this.ps.setObject(1, userName);
            this.ps.setObject(2, password);
            list = this.getObject(this.ps.executeQuery());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        if (list.size()==0){
            return null;
        }else {
            return  (UserBean)list.get(0);
        }
    }

    @Override
    public void update(String code, double account) {
        this.setConnection();
        try {
            this.ps = this.connection.prepareStatement("update t_users set u_account = u_account + ? where pk_name = ? ");
            this.ps.setObject(1, account);
            this.ps.setObject(2, code);
            this.ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

    }

    @Override
    public UserBean findByName(String Code) {
        List list = null;
        this.setConnection();
        try {

            this.ps = this.connection.prepareStatement("select * from t_users where pk_name = ? ");
            this.ps.setObject(1, Code);
            list = this.getObject(this.ps.executeQuery());

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        if (list.size()==0){
            return null;
        }else {
            return (UserBean)list.get(0);
        }
    }
}
